//
//  FPForgetSecretViewController.m
//  ForestPack
//
//  Created by jimmy on 2018/7/4.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "FPForgetSecretViewController.h"
#import "FPRegisAndForgetView.h"
#import "UIView+Utils.h"
#import "FPLoginViewController.h"


@interface FPForgetSecretViewController ()

@property (nonatomic,strong)  UILabel   * textLabel;
@property (nonatomic,strong)  UIButton  * loginButton;
@property (nonatomic,strong)  UIButton  * submitButton;
@property (nonatomic,strong)  FPRegisAndForgetView  * forgetView;

@end

@implementation FPForgetSecretViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTopViewUI];
    [self setViewUI];
    
}

-(void)uploadData
{
    NSString *phoneNumStr = self.forgetView.phoneNumTextField.text;
    if (phoneNumStr.length <= 0 && phoneNumStr.length != 11) {
        [HttpManager showNoteMsg:@"请输入正确的手机号"];
        return;
    }
    if (self.forgetView.messageTextField.text.length <= 0 && [FPLoginAndRegisterTools checkYzmField:self.forgetView.messageTextField.text]) {
        [HttpManager showNoteMsg:@"请输入验证码"];
        return;
    }
    if (self.forgetView.passwordTextField.text.length <= 0) {
        [HttpManager showNoteMsg:@"请输入密码"];
        return;
    }
    if (self.forgetView.passwordTextField.text != self.forgetView.confirmPasswordTextField.text) {
        [HttpManager showNoteMsg:@"两次输入的密码不相同，请重新输入"];
        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"%@app/reset_password",REQUESTHEADER];//添加商品
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setValue:self.forgetView.phoneNumTextField.text forKey:@"mobile"];
    [dict setValue:self.forgetView.messageTextField.text forKey:@"code"];
    [dict setValue:self.forgetView.passwordTextField.text forKey:@"password"];
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            [HttpManager showNoteMsg:@"修改成功"];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                FPLoginViewController *loginVC = [[FPLoginViewController alloc] init];
                [self.navigationController pushViewController:loginVC animated:true];
            });
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
    
}

-(void)backClick
{
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark - 点击进入登录界面
-(void)LoginClick
{
    FPLoginViewController *loginVC = [[FPLoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:true];
}

#pragma mark - 点击提交
-(void)submitClick
{
    [self uploadData];
    
}

#pragma mark - 设置头部View
-(void)setTopViewUI
{
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(13, APP_STATUSBAR_HEIGHT, 22, 22)];
    [leftButton setImage:[UIImage imageNamed:@"返回红色"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.centerX = self.view.centerX;
    topImageView.size = CGSizeMake(80, 80);
    topImageView.left = (SCREEN_WIDTH - 80) / 2;
    topImageView.top = NAVIGATION_BAR_HEIGHT + 10;
    topImageView.image = [UIImage imageNamed:@"组11"];
    [self.view addSubview:topImageView];
    
    self.textLabel = [[UILabel alloc] init];
    self.textLabel.frame = CGRectMake((SCREEN_WIDTH - 150)/2, topImageView.bottom + 20, 150, 20);
    self.textLabel.font = [UIFont systemFontOfSize:18];
    self.textLabel.textColor = RGB_COLOR(51, 51, 51);
    self.textLabel.text = @"找回密码";
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.textLabel];
    
    self.loginButton = [[UIButton alloc] init];
    self.loginButton.frame = CGRectMake(SCREEN_WIDTH - 80, NAVIGATION_BAR_HEIGHT + 5, 65, 30);
    self.loginButton.layer.masksToBounds = true;
    self.loginButton.layer.cornerRadius = 15;
    self.loginButton.layer.borderWidth = 0.5;
    self.loginButton.layer.borderColor = RGB_COLOR(237, 34, 59).CGColor;
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(LoginClick) forControlEvents:UIControlEventTouchUpInside];
    self.loginButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.loginButton setTitleColor:RGB_COLOR(237, 34, 59) forState:UIControlStateNormal];
    [self.view addSubview:self.loginButton];
    
}

-(void)setViewUI
{
    self.forgetView = [[FPRegisAndForgetView alloc] init];
    self.forgetView.frame = CGRectMake(0, self.textLabel.bottom + 30, SCREEN_WIDTH, 240);
    self.forgetView.isMsgRegist = NO;
    [self.view addSubview:self.forgetView];
    
    self.submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.submitButton.backgroundColor = RGB_COLOR(237, 34, 59);
    self.submitButton.frame = CGRectMake(40, self.forgetView.bottom + 40, SCREEN_WIDTH - 80, 44);
    self.submitButton.layer.masksToBounds = true;
    self.submitButton.layer.cornerRadius = 22;
    [self.submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [self.submitButton addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.submitButton];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:true];
}

@end
